/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.sind.blip.entities;

import java.text.ParseException;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

public class Shortlink extends Entity{
	
	private Long id;
	private Date createdAt;
	private String shortcode;
	private int hitCount;
	private String originalLink;
	public static final String SHORT_LINK_ROOT="http://rdir.pl/";
	
	public Shortlink(JSONObject jsonObject) throws JSONException {
		id = jsonObject.optLong("id");
		try {
			createdAt = stringToDate(jsonObject.optString("created_at"));
		} catch (ParseException e) {
			//just ignore for now
		}
		shortcode = jsonObject.optString("shortcode");
		originalLink = jsonObject.optString("original_link");
		hitCount = jsonObject.optInt("hit_count");
	}
	
	public Shortlink(){
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public String getShortcode() {
		return shortcode;
	}

	public void setShortcode(String shortcode) {
		this.shortcode = shortcode;
	}

	public int getHitCount() {
		return hitCount;
	}

	public void setHitCount(int hitCount) {
		this.hitCount = hitCount;
	}

	public String getOriginalLink() {
		return originalLink;
	}

	public void setOriginalLink(String originalLink) {
		this.originalLink = originalLink;
	}

	/**
	 * Returns full shortlink.
	 * 
	 * @return  {@link String} with http address
	 */
	public String getFullShortLink(){
		return SHORT_LINK_ROOT+shortcode;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((createdAt == null) ? 0 : createdAt.hashCode());
		result = prime * result + hitCount;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((originalLink == null) ? 0 : originalLink.hashCode());
		result = prime * result
				+ ((shortcode == null) ? 0 : shortcode.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Shortlink other = (Shortlink) obj;
		if (createdAt == null) {
			if (other.createdAt != null)
				return false;
		} else if (!createdAt.equals(other.createdAt))
			return false;
		if (hitCount != other.hitCount)
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (originalLink == null) {
			if (other.originalLink != null)
				return false;
		} else if (!originalLink.equals(other.originalLink))
			return false;
		if (shortcode == null) {
			if (other.shortcode != null)
				return false;
		} else if (!shortcode.equals(other.shortcode))
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append(Shortlink.class.getName()).append("{");
		b.append("id: ").append(id);
		b.append(", createdAt: ").append(createdAt);
		b.append(", originalLink: ").append(originalLink);
		b.append(", shortcode: ").append(shortcode);
		b.append(", hitCount: ").append(hitCount);
		b.append("}");
		return b.toString();
	}
	
}
